"""
WebSocket路由
"""
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from backend.services.websocket_service import websocket_manager

router = APIRouter(tags=["WebSocket"])

@router.websocket("/ws/logs")
async def websocket_logs(websocket: WebSocket):
    """WebSocket连接用于实时日志推送"""
    await websocket_manager.connect(websocket)
    
    try:
        # 保持连接直到客户端断开
        while True:
            # 接收客户端消息（心跳检测）
            data = await websocket.receive_text()
            
            # 可以在这里处理客户端发来的消息
            # 比如心跳检测、日志过滤参数等
            
    except WebSocketDisconnect:
        websocket_manager.disconnect(websocket)
    except Exception as e:
        print(f"WebSocket错误: {e}")
        websocket_manager.disconnect(websocket) 